﻿using PhotoLabs.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace PhotoLabs.DataModel
{
    public class PhotoDataItem : SampleDataCommon
    {
        public PhotoDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String location,String photographer, PhotoDataGroup group)
            : base(uniqueId, title, subtitle, imagePath, description)
        {
            this._location = location;
            this._photographer = photographer;
            this._group = group;
            
        }

        public PhotoDataItem(String uniqueId, String title, String subtitle, String imagePath,String tileImagePath, String description, String location, String photographer, PhotoDataGroup group)
            : base(uniqueId, title, subtitle, imagePath, description)
        {
            this._location = location;
            this._photographer = photographer;
            this._group = group;
            this._tileImagePath = tileImagePath;

        }

        private string _tileImagePath;

        public Uri TileImagePath
        {
            get
            {
                return new Uri(SampleDataCommon._baseUri, this._tileImagePath);
            }

        }

        public Uri ImagePath
        {
            get
            {
                return new Uri(SampleDataCommon._baseUri, this._imagePath);
            }
        } 

        private string _location = string.Empty;
        public string Location
        {
            get { return this._location; }
            set { this.SetProperty(ref this._location, value); }
        }

        private string _photographer = string.Empty;
        public string Photographer
        {
            get { return this._photographer; }
            set { this.SetProperty(ref this._photographer, value); }
        }

        public string Quote
        {
            get { return "\""+Description+"\""; }
        }

        private PhotoDataGroup _group;
        public PhotoDataGroup Group
        {
            get { return this._group; }
            set { this.SetProperty(ref this._group, value); }
        }
    }

    public class PhotoDataGroup : SampleDataCommon
    {
        public PhotoDataGroup(String uniqueId, String title, String subtitle, String imagePath, String description)
            : base(uniqueId, title, subtitle, imagePath, description)
        {
            Items.CollectionChanged += ItemsCollectionChanged;
        }

        private void ItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Provides a subset of the full items collection to bind to from a GroupedItemsPage
            // for two reasons: GridView will not virtualize large items collections, and it
            // improves the user experience when browsing through groups with large numbers of
            // items.
            //
            // A maximum of 12 items are displayed because it results in filled grid columns
            // whether there are 1, 2, 3, 4, or 6 rows displayed

            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    if (e.NewStartingIndex < 12)
                    {
                        TopItems.Insert(e.NewStartingIndex, Items[e.NewStartingIndex]);
                        if (TopItems.Count > 12)
                        {
                            TopItems.RemoveAt(12);
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Move:
                    if (e.OldStartingIndex < 12 && e.NewStartingIndex < 12)
                    {
                        TopItems.Move(e.OldStartingIndex, e.NewStartingIndex);
                    }
                    else if (e.OldStartingIndex < 12)
                    {
                        TopItems.RemoveAt(e.OldStartingIndex);
                        TopItems.Add(Items[11]);
                    }
                    else if (e.NewStartingIndex < 12)
                    {
                        TopItems.Insert(e.NewStartingIndex, Items[e.NewStartingIndex]);
                        TopItems.RemoveAt(12);
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    if (e.OldStartingIndex < 12)
                    {
                        TopItems.RemoveAt(e.OldStartingIndex);
                        if (Items.Count >= 12)
                        {
                            TopItems.Add(Items[11]);
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Replace:
                    if (e.OldStartingIndex < 12)
                    {
                        TopItems[e.OldStartingIndex] = Items[e.OldStartingIndex];
                    }
                    break;
                case NotifyCollectionChangedAction.Reset:
                    TopItems.Clear();
                    while (TopItems.Count < Items.Count && TopItems.Count < 12)
                    {
                        TopItems.Add(Items[TopItems.Count]);
                    }
                    break;
            }
        }

        private ObservableCollection<PhotoDataItem> _items = new ObservableCollection<PhotoDataItem>();
        public ObservableCollection<PhotoDataItem> Items
        {
            get { return this._items; }
        }

        private ObservableCollection<PhotoDataItem> _topItem = new ObservableCollection<PhotoDataItem>();
        public ObservableCollection<PhotoDataItem> TopItems
        {
            get { return this._topItem; }
        }

        private ImageSource _groupImage;
        private string _groupImagePath;

        public ImageSource GroupImage
        {
            get
            {
                if (this._groupImage == null && this._groupImagePath != null)
                {
                    this._groupImage = new BitmapImage(new Uri(SampleDataCommon._baseUri, this._groupImagePath));
                }
                return this._groupImage;
            }
            set
            {
                this._groupImagePath = null;
                this.SetProperty(ref this._groupImage, value);
            }
        }

        public int PhotoCount
        {
            get
            {
                return this.Items.Count;
            }
        }

        public void SetGroupImage(String path)
        {
            this._groupImage = null;
            this._groupImagePath = path;
            this.OnPropertyChanged("GroupImage");
        }

    }

    public sealed class PhotoDataSource
    {
        private static PhotoDataSource _photoDataSource = new PhotoDataSource();

        private ObservableCollection<PhotoDataGroup> _allGroups = new ObservableCollection<PhotoDataGroup>();
        public ObservableCollection<PhotoDataGroup> AllGroups
        {
            get { return this._allGroups; }
        }

        public static IEnumerable<PhotoDataGroup> GetGroups(string uniqueId)
        {
            if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");

            return _photoDataSource.AllGroups;
        }

        public static PhotoDataGroup GetGroup(string uniqueId)
        {
            // Simple linear search is acceptable for small data sets
            var matches = _photoDataSource.AllGroups.Where((group) => group.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }

        public static PhotoDataItem GetItem(string uniqueId)
        {
            // Simple linear search is acceptable for small data sets
            var matches = _photoDataSource.AllGroups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }

        public PhotoDataSource()
        {
            
            var group1 = new PhotoDataGroup("Group-1",
                    "Bandung",
                     String.Empty,
                    "Images/Bandung_Group.png",
                    "Bandung is the capital of West Java province in Indonesia, the country's third largest city, and second largest metropolitan area in Indonesia[1] with a population of 2.4 million in 2010. Located 768 meters (2,520 ft) above sea level, approximately 140 km south eastern of Jakarta, Bandung has cooler temperatures year-round than most other Indonesian cities. ");
            group1.Items.Add(new PhotoDataItem("Bandung_001",
                    "Early Sunset",
                    String.Empty,
                    "Images/Bandung-EarlySunset.png",
                    "Images/Bandung-EarlySunset_150.png",
                    "Early sunset in Bandung Padasuka",
                    "Padasuka",
                     "M Reza Faisal",
                    group1));
            group1.Items.Add(new PhotoDataItem("Bandung_002",
                    "Top of the world",
                    String.Empty,
                    "Images/Bandung-TopOfTheWorld.png",
                    "Images/Bandung-TopOfTheWorld_150.png",
                    "Standing on top of the world,you and sunset",
                    "Caringin Tilu",
                    "M Reza Faisal",
                    group1));
            group1.Items.Add(new PhotoDataItem("Bandung_003",
                    "Tree of Love",
                    String.Empty,
                    "Images/Bandung-TreeOfLove.png",
                    "Images/Bandung-TreeOfLove_150.png",
                    "The trees are golden by the sun",
                    "Caringin Tilu",
                    "M Reza Faisal",
                    group1));
            
            this.AllGroups.Add(group1);

            var group2 = new PhotoDataGroup("Group-2",
                    "East Java",
                    String.Empty,
                    "Images/EastJava_Group.png",
                    "East Java (Indonesian: Jawa Timur, Javanese: Jawa Wétan) is a province of Indonesia. It is located on the eastern part of the island of Java and includes the neighbouring island of Madura and islands to its east (the Kangean and Sapudi groups) and to its north (Bawean and the Masalembu Islands. It has a land border only with Central Java (Jawa Tengah) province to its west.");
            group2.Items.Add(new PhotoDataItem("EastJava_001",
                    "Girl in Purple",
                    String.Empty,
                    "Images/EastJava-Jember.png",
                    "Images/EastJava-Jember_150.png",
                    "Girl,purple and silent forest",
                    "Jember",
                    "M Reza Faisal",
                    group2));

            group2.Items.Add(new PhotoDataItem("EastJava_002",
                    "Night at Bromo",
                    String.Empty,
                    "Images/EastJava-NightAtBromo.png",
                    "Images/EastJava-NightAtBromo_150.png",
                    "Dessert of sand at night,feels like home",
                    "Mount Bromo",
                    "M Reza Faisal",
                    group2));
            group2.Items.Add(new PhotoDataItem("EastJava_003",
                    "Papuma Beach",
                    String.Empty,
                    "Images/EastJava-PapumaBeach.png",
                    "Images/EastJava-PapumaBeach_150.png",
                    "wind at the beach of Papuma,children will like to play",
                    "Papuma",
                    "M Reza Faisal",
                    group2));
            group2.Items.Add(new PhotoDataItem("EastJava_004",
                    "Slow Heaven",
                    String.Empty,
                    "Images/EastJava-SlowHeaven.png",
                    "Images/EastJava-SlowHeaven_150.png",
                    "Miss the beach already, the water poured hard",
                    "Papuma",
                    "M Reza Faisal",
                    group2));
            this.AllGroups.Add(group2);

            var group3 = new PhotoDataGroup("Group-3",
                    "Borneo",
                    String.Empty,
                    "Images/Kalimantan_Group.png",
                    "Borneo is surrounded by the South China Sea to the north and northwest, the Sulu Sea to the northeast, the Celebes Sea and the Makassar Strait to the east, and the Java Sea and Karimata Strait to the south. With an area of 743,330 square kilometres (287,000 sq mi), it is the largest island in Maritime Southeast Asia. To the west of Borneo are the Malay Peninsula and Sumatra. ");
            group3.Items.Add(new PhotoDataItem("Borneo_001",
                    "I Will Be Beside You",
                    String.Empty,
                    "Images/Kalimantan-IWillBeBesideYou.png",
                    "Images/Kalimantan-IWillBeBesideYou_150.png",
                    "a family on the boat and silent river",
                    "South Borneo",
                    "M Reza Faisal",
                    group3));
            group3.Items.Add(new PhotoDataItem("Borneo_002",
                    "Morning Glory",
                    String.Empty,
                    "Images/Kalimantan-MorningGlory-JourneyToLokBaintan.png",
                    "Images/Kalimantan-MorningGlory-JourneyToLokBaintan_150.png",
                    "Journey to Lok Baintan",
                    "Lok Baintan",
                    "M Reza Faisal",
                    group3));
            group3.Items.Add(new PhotoDataItem("Borneo_003",
                    "Ramazan Bayrami",
                    String.Empty,
                    "Images/Kalimantan-Ramazan_Bayrami-Mohonmaaflahirbatin.png",
                    "Images/Kalimantan-Ramazan_Bayrami-Mohonmaaflahirbatin_150.png",
                    "Mohon maaf lahir batin",
                    "Lok Baintan",
                    "M Reza Faisal",
                    group3));
          
            this.AllGroups.Add(group3);

            var group4 = new PhotoDataGroup("Group-4",
                    "Turkey",
                    String.Empty,
                    "Images/Turkey_Group.png",
                    "Turkey  is a Eurasian country located in Western Asia (mostly in the Anatolian peninsula) and in East Thrace in Southeastern Europe");
            group4.Items.Add(new PhotoDataItem("Turkey_001",
                    "Time after time",
                    String.Empty,
                    "Images/Turkey-TimeafterTime.png",
                    "Images/Turkey-TimeafterTime_150.png",
                    "rocky mountain and balloon",
                    "Turkey",
                    "M Reza Faisal",
                    group4));
            group4.Items.Add(new PhotoDataItem("Turkey_002",
                    "Today is Holiday",
                    String.Empty,
                    "Images/Turkey-TodayIsHoliday.png",
                    "Images/Turkey-TodayIsHoliday_150.png",
                    "two balloon in your holiday",
                    "Rocky Mountain, Turkey",
                    "M Reza Faisal",
                    group4));
            group4.Items.Add(new PhotoDataItem("Turkey_003",
                    "Necessary Evil",
                    String.Empty,
                    "Images/Turkey-NecessaryEvil.png",
                    "Images/Turkey-NecessaryEvil_150.png",
                    String.Empty,
                    "Turkey Temple",
                    "M Reza Faisal",
                    group4));
            
            this.AllGroups.Add(group4);

            
        }
    }
}
